// pages/report/report.js
const api=require('../../utils/request') 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareModel:{
        title:'',
        path:'pages/test/test', 
        fromButton:'one'
      },  
      isReport: false,
    tabNavActive:0,
    tabNav:["推荐","热榜"],
    recommendData:[],
    todaySell: '',
    ranking:'',
    pageNum:1,
    pageSize: 10,
    isLoading: false, // 是否正在加载更多
    isRefreshing: false, // 是否正在刷新
    noData: false, // 没有更多数据了
    FX:false,
    userInfo: null,
    pages:0,
    gdLoading:true,
    isClose:false,
    isRead: false,
    reportData:[],
    posterId:'',
    name:'',
    image:'',
    isAfterShow:true,
    posterList:[],
    array: ['行业','柴油', '汽油', ],
    index: 0,
    pickerStatus:0,
    industryList: [],
    industryId: null
  },
  //行业切换点击状态
  bindPickerClick(){
    this.setData({
      pickerStatus:1,
      tabNavActive:null
    })
  },
  industryListFunction(){
      let that=this
      api.get('web/AllIndustryList').then(response=>{
             if(response.code===200){
                 let array=[{name:'行业'}]
              that.setData({
                industryList: array.concat(response.data)
              })
             }else{
                
             }
      })
  },
  //行业切换
  bindPickerChange: function (e) {
     this.setData({
      index: e.detail.value,
      todaySell: '',
            ranking:'',
  });
  if(e.detail.value!=='0'){
    let item= this.data.industryList[e.detail.value]
     this.setData({
        industryId: item.id
    })
  }else{
    this.setData({
        industryId: null
    }) 
  }
  this.loadData(1)
  },
  posterData(){
      let that=this
      api.get('web/posterList/2').then(response=>{ 
          if(response.code===200 && response.data!==null){
            that.setData({
                posterList: response.data
            })
          }
      })
  },
  //关闭登陆后弹框
  loginAfterClose(){
    this.setData({
      isAfterShow:false
    })
  },
  kf(){
    //
   wx.openCustomerServiceChat({
       extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcdb659362f1afb3c0'},
       corpId: 'wwbb8c2c0fafdbf12b',
       success(res) {
           console.log('resss',res)
       }
     })
  },
  //回顶部
  toTop(){
		wx.pageScrollTo({
		    scrollTop: 0
		})
	},
  //保存海报
  save_report(){
    let userInfo= wx.getStorageSync('user')
    if(userInfo!==''){
        let user= JSON.parse(userInfo) 
        let reportId=this.data.posterId
         api.post('weChatMini/saveCollect',{memberId: user.id,reportId:reportId}).then(res=>{
            if(res.code===200){
                wx.showToast({
                    title: '收藏成功',
                    icon: 'none',
                    duration: 2000,
                  })
                  
            }
        })
    }else{
        wx.showToast({
            title: '请登录',
            icon: 'none',
            duration: 2000,
          })
    }
   
    
  },
  //分享
  fxWrap(e){  
    this.setData({
      FX:true,
      posterId: e.currentTarget.dataset.id,
      name: e.currentTarget.dataset.name,
      image: e.currentTarget.dataset.image
    })
  },
  reportClonse(){
      this.setData({
        isReport: true
      })
  },
  reportBind(e){
      console.log('e',e)
    wx.openOfficialAccountArticle({
        url:e.currentTarget.id, // 此处填写公众号文章连接
        success: res => {
        },
        fail: res => {
        }
    })
  },
  copy(data){
     let reportId= this.data.posterId 
     wx.setClipboardData({
       data: 'https://www.xinyanbao.cn/#/pdf/?id='+reportId,
       success:function(res){ 
       }
     })
  },

  //客服关闭
  close(){
    this.setData({
      isClose:true
    });
  },
  fxCancel(){
    this.setData({
      FX:false
    })
  },
  zan(e){
      
    let id=e.currentTarget.id
    let index= e.currentTarget.dataset.index 
    let data=this.data.recommendData
    let that=this
    let fasg=data[index].isSelected
    if(data[index].isSelected){
        data[index].browseNum=data[index].browseNum+1
    }else{
        data[index].browseNum=data[index].browseNum-1 
    }
    data[index].isSelected=!data[index].isSelected
    api.post('weChatMini/saveRecommend',{id: id ,isSelected: fasg}).then(res=>{
        if(res.code===200){
            that.setData({
                recommendData: data
            })
        }
    })
    
  },
  changeSearch(e){
       wx.navigateTo({
        url: '../report_tag/report_tag?industryId='+e.currentTarget.dataset.id,
      })
  },
  //下拉刷新
  handleRefresh(){
        
    if (this.data.isRefreshing) return;
    this.setData({
      isRefreshing: true,
      noData: false,
      pageNum: 1,
      gdLoading: true
    },()=>{
      //请求数据
    })
    this.loadData(1)
    setTimeout(()=>{
      this.setData({
        isRefreshing: false,
        noData: false
      })
    },2000)
  },
  // 加载更多
  loadMore(){
    console.log("加载更多");
    if(this.data.isLoading) return;
    // this.setData({
    //   noData: true
    // }) 
  },
  changeData(){
    if(this.data.pages>this.data.pageNum){
       this.setData({
           pageNum: this.data.pageNum+1
       })
       this.loadData(2)
   }else{
      this.setData({
        gdLoading: false,
        noData: true
      })
   }
  },
  join_subscribe_to(){
    wx.navigateTo({
      url: './subscribe_to/subscribe_to',
    })
  },
  pdf(){
    wx.navigateTo({
      url: './search_res/searchc_res',
    })
  },
  subscribeLists(){
    let userInfo= wx.getStorageSync('user')
    let user= JSON.parse(userInfo) 
    let that=this;
      api.post('weChatMini/subscribeLists',{memberId:user.id}).then(response=>{
         if(response.code===200){
             console.log('rrrrr',response.data)
             if(response.data===null){
                that.setData({
                    isRead: true
                })
             }else{
            that.setData({
                reportData: response.data,
                isRead: false
            })
             }
         }
      })
  },

  tabNavClick(e){
 
    this.setData({
        tabNavActive : e.currentTarget.dataset.id,
        recommendData:[],
        pageNum: 1,
        FX: false,
        pickerStatus:0,
      })
       if(e.currentTarget.dataset.id===0){
          this.setData({
            todaySell: 1,
            ranking:'',
            pageSize: 10
          })
          this.loadData(1)
      }else if(e.currentTarget.dataset.id===1){
        this.setData({
            todaySell: '',
            ranking:1,
            pageSize: 50
          })
          this.loadData(1)
      } else if(e.currentTarget.dataset.id===2){
           this.subscribeLists()
      }
    
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.industryListFunction()
    this.posterData()
       if(this.data.tabNavActive===2){
          this.subscribeLists()
       }else{
        this.setData({
            todaySell: 1,
            ranking:'' 
          })
         this.loadData(1)
       
       }
        
   
    

     
 
  },
  setInput(e){
    console.log(123);
    wx.navigateTo({
      url: './search/search',
    })
  },
 
loadData(val){
    
    let that=this
    let memberId=null
    let userInfo= wx.getStorageSync('user')
    if(userInfo!==''){
     let user= JSON.parse(userInfo)
      memberId=user.id
    }
    
     api.post('weChatMini/recommendLists',{name:this.data.name,memberId:memberId,ranking:this.data.ranking,todaySell:this.data.todaySell,pageNum:this.data.pageNum,pageSize:this.data.pageSize,industryId: this.data.industryId}).then(res=>{        
            if(res.code===200){
               if( res.data.records.length===0){
                   that.setData({
                    noData: true,
                    gdLoading: false
                   })
               }else{
                that.setData({
                    noData: false,
                    gdLoading: true
                   })
               }
               res.data.records.forEach(item=>{
                   item.isSelected=true
               })

                if(val===2){
                let data=that.data.recommendData.concat(res.data.records)
                that.setData({
                    recommendData:data,
                    pages: res.data.pages
                })    
              }else{
                    that.setData({
                        recommendData:res.data.records,
                        pages: res.data.pages
                    })
                }
                
            }
     })
},
changePdf(e){
    let userInfo= wx.getStorageSync('user')
  
    let id=e.currentTarget.dataset.id 
    wx.navigateTo({
        // 普通页面
        url:'./pdf/pdf?id='+id+'&backType=1'
      })
   
    
},
addSubscribe(){
wx.navigateTo({
  url: './subscribe_to/subscribe_to',
})
},
//联系客服
kf(){
  
 wx.openCustomerServiceChat({
     extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcdb659362f1afb3c0'},
     corpId: 'wwbb8c2c0fafdbf12b',
     success(res) {
         console.log('resss',res)
     }
   })
},
//删除订阅
subscribe_del(e){
    console.log('eeee',e)
    let that=this
    let index=e.currentTarget.dataset.index
  wx.showModal({  
    title: '确认',  
    content: '确定要删除吗？',  
    success (res) {  
      if (res.confirm) {  
         console.log('eee',e)
         
         let id=e.currentTarget.dataset.id
         api.get('weChatMini/deleteSubscribe/'+id).then(response=>{
            if(response.code===200){
                let data=that.data.reportData
                data.splice(index,1)
                that.setData({
                    reportData: data
                })
            }
         })
      } else if (res.cancel) {  
        console.log('用户点击取消')  
      }  
    }  
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    his.setData({
        recommendData: [], // 清空数据
        pageNum: 1 // 重置页数
      });
      this.loadData(1); // 重新加载数据
      wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
       if(this.data.tabNavActive===0){
        if(this.data.pages>this.data.pageNum){
            this.setData({
                pageNum: this.data.pageNum+1
            })
            this.loadData(2)
        }else{
           this.setData({
             gdLoading: false,
             noData: true
           })
        }
      }
   
   },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
      
      if(res.from==='button' ){
        if(res.target.dataset.shareinfo!==undefined){
            this.setData({
                FX: false
            })
            return {
              title: this.data.name,
              path:'/pages/report/pdf/pdf?id='+this.data.posterId,
              imageUrl: this.data.image
            }
        }else{
            return {
                title: '新研报',
                path:'/pages/report/report',
                imageUrl: 'https://yw.xinyanbao.cn/2024-07-04/WechatIMG2171.jpg'                
              } 
        }
          
      }
      
  },
  onShareTimeline:function(res){
    return {
        title: this.title,
        imageUrl: this.data.image,
    }

  }

   
})